package com.iven.xie.encrypt.controller;

import com.iven.xie.encrypt.bean.EncryptBody;
import com.iven.xie.encrypt.bean.Result;
import com.iven.xie.encrypt.tool.DecryptRequestBodyService;
import com.iven.xie.encrypt.tool.EncryptResponseBodyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author iven.xie
 */
@Api(tags = "加密接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("api/customer")
@Slf4j
public class EncryptController {

    private final Logger logger = LoggerFactory.getLogger(EncryptController.class);

    @Autowired
    private EncryptResponseBodyService encryptResponseBodyService;

    @Autowired
    private DecryptRequestBodyService decryptRequestBodyService;

    /**
     * 对请求进行加密的方法
     *
     * @param encryptBody 请求数据
     * @return encryptBody
     */
    @ApiOperation(value = "对请求数据进行加密的方法", notes = "对请求数据进行加密的方法")
    @PostMapping("/encrypt")
    public Result<EncryptBody> encryptRequest(@RequestBody EncryptBody encryptBody) {
        logger.info("对请求数据进行加密的方法,入参:", encryptBody);
        //通过加密工具获得加密后的请求数据
        String encryptBodyStr = encryptResponseBodyService.switchEncrypt(encryptBody);
        encryptBody.setEncryptData(encryptBodyStr);
        return new Result<>(HttpStatus.OK.value(), encryptBody);
    }

    /**
     * 对响应进行解密的方法
     *
     * @return decodeBody
     */
    @ApiOperation(value = "对响应进行解密的方法", notes = "对响应进行解密的方法")
    @PostMapping("decode")
    public Result<EncryptBody> decodeResponse(@RequestBody EncryptBody decodeBody) {
        logger.info("对响应进行解密的方法,入参:", decodeBody);
        //通过解密工具获得解密后的请求数据
        String decodeBodyStr = decryptRequestBodyService.switchDecrypt(decodeBody);
        decodeBody.setDecodeData(decodeBodyStr);
        return new Result<>(HttpStatus.OK.value(), decodeBody);
    }
}
